import java.util.*;

public class Leet39 {
    public static void main(String[] args) {
        Leet39 l = new Leet39();
        System.out.println(l.combinationSum(new int[]{2,3,6,7},7));
    }
    static Set<List<Integer>> list;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        list = new HashSet<>();
        dfs(candidates,target,0,new ArrayList<>());
        return new ArrayList<>(list);
    }
    void dfs(int[] can,int target,int num,List<Integer> temp){
        if (num==target){
            List<Integer> l = new ArrayList<>(temp);
            Collections.sort(l);
            list.add(l);
            return;
        }
        if (num>target)
            return;
        for (int i = 0; i < can.length; i++) {
            temp.add(can[i]);
            dfs(can,target,num+can[i],temp);
            temp.remove(temp.size()-1);
        }
    }
}
